﻿using System;
using PBO.Data;
using PBO.Network;
using System.Linq;
using PBO.Game;
using PBO.Game.Img;
using PBO.Network.Enumerations;

namespace PBO.Game.BattleClass
{
    public partial class Battle
    {

        private void AssistUser(Move selMove, Pokemon user)
        {
            switch (selMove.Data.Effect)
            {
                case MoveEffect.诅咒:
                    user.AbilityLvUpChanged(StatType.Atk, 1, false);
                    user.AbilityLvUpChanged(StatType.Def, 1, false);
                    //user.AtkLVUp(1);
                    //user.DefLVUp(1);
                    user.AbilityLvDownChanged(StatType.Speed, 1, true);
                    break;
                case MoveEffect.殊途同归:
                    user.BattleState.destinyBond = true;
                    break;
                case MoveEffect.保护:
                    if (atkList.Count != 0 && user.BattleState.protectCounter < 6 && this.OneNth(1 << user.BattleState.protectCounter))
                    {
                        user.BattleState.protect = true;
                        log.AppendMsg("EnProtect", user.CustomName);
                        //log.AddText(user.CustomName + "进入了保护范围！");
                        user.BattleState.protectCounter++;
                    }
                    else
                    {
                        user.BattleState.protectCounter = 0;
                        Fail();
                    }
                    break;
                case MoveEffect.忍耐:
                    if (user.BattleState.protectCounter < 6 && this.OneNth(1 << user.BattleState.protectCounter))
                    {
                        user.BattleState.endure = true;
                        log.AppendMsg("EnEndure", user.CustomName);
                        //log.AddText(user.CustomName + "忍耐了！");
                        user.BattleState.protectCounter++;
                    }
                    else
                    {
                        user.BattleState.protectCounter = 0;
                        Fail();
                    }
                    break;
                case MoveEffect.魔装反射:
                    user.BattleState.magicCoat = true;
                    break;
                case MoveEffect.休息:

                    if (user.State == PokemonState.Sleep || Ground.Uproar || user.GetSelTrait() == Trait.Insomnia || user.GetSelTrait() == Trait.VitalSpirit)
                    {
                        Fail();
                        return;
                    }
                    if (!user.CheckIfHPRecoveryIsEnabled())
                    {
                        return;
                    }
                    if (user.RecoverPokemonHP(user.MaxHp))
                    {
                        user.State = PokemonState.Sleep;
                        log.AppendMsg("EnSLP", user.CustomName);
                        //log.AddText(user.CustomName + "睡着了！");
                        user.sleepTurn = RestTurn;
                        if (user.GetSelTrait() == Trait.EarlyBird)
                            user.sleepTurn = RestTurn / 2;
                        user.sleepCounter = 0;
                        if (user.GetItem() == Item.ChestoBerry || user.GetItem() == Item.LumBerry)
                        {
                            user.RaiseItem();
                        }
                    }
                    break;
                case MoveEffect.HP一半恢复:
                    user.RecoverPokemonHP(user.MaxHp >> 1);
                    break;
                case MoveEffect.鸟栖:
                    if (user.RecoverPokemonHP(user.MaxHp >> 1))
                    {
                        if (user.HaveType(BattleType.Flying))
                        {
                            user.BattleState.roost = true;
                            user.BattleState.RoostType = BattleType.None;
                            //if (user.Type2 == BattleType.None)
                            //{
                            //    user.BattleState.RoostType = BattleType.Normal;
                            //}
                            //else
                            //{
                            //    user.BattleState.RoostType = BattleType.None;
                            //}
#warning 羽栖 log
                            log.AppendText(user.CustomName + "的飞行属性暂时消失了");
                        }
                    }
                    break;
                case MoveEffect.HP天气恢复:
                    int recovery = 0;
                    switch (Ground.Weather)
                    {
                        case Weather.Normal:
                            recovery = user.MaxHp >> 1;
                            break;
                        case Weather.Sunny:
                            recovery = user.MaxHp * 2 / 3;
                            break;
                        default:
                            recovery = user.MaxHp >> 2;
                            break;
                    }
                    user.RecoverPokemonHP(recovery);
                    break;
                case MoveEffect.许愿:
                    if (!user.CheckIfHPRecoveryIsEnabled())
                    {
                        return;
                    }
                    if (!user.BattleState.wished)
                    {
                        user.BattleState.wished = true;
                        user.BattleState.wishTurn = -1;
                        //user.BattleState.wishRecovery = user.MAXHP / 2;
                        //log.AppendText(user.CustomName + "许下愿望！");
                    }
                    else
                    {
                        Fail();
                    }
                    break;
                case MoveEffect.接力:
                    if (!user.Pass())
                        Fail();
                    break;
                case MoveEffect.自身状态恢复:
                    if (user.State == PokemonState.Burn || user.State == PokemonState.Paralysis || user.State == PokemonState.Poison || user.State == PokemonState.Toxin)
                    {
                        user.State = PokemonState.No;
                        log.AppendMsg("StateReset", user.CustomName);
                        //log.AddText(user.CustomName + "的状态恢复了！");
                    }
                    break;
                case MoveEffect.回避加一:
                    user.AbilityLvUpChanged(StatType.Evasion, 1, false);
                    //user.EvasionLVUp(1);
                    break;
                case MoveEffect.变小:
                    user.BattleState.minimize = true;
                    user.AbilityLvUpChanged(StatType.Evasion, 2, false);
                    //user.EvasionLVUp(2);
                    break;
                case MoveEffect.蓄力:
                    if (user.BattleState.FocusEngery == false)
                    {
                        user.CTLVUp(2);
                        user.BattleState.FocusEngery = true;
                        log.AppendMsg("EnFocusPunch", user.CustomName);
                        //log.AddText(user.CustomName + "在积蓄力量！");
                    }
                    else
                    {
                        Fail();
                    }
                    break;
                case MoveEffect.速度加一:
                    user.AbilityLvUpChanged(StatType.Speed, 1, false);
                    //user.SpeedLVUp(1);
                    break;
                case MoveEffect.速度加二:
                    user.AbilityLvUpChanged(StatType.Speed, 2, false);
                    //user.SpeedLVUp(2);
                    break;
                case MoveEffect.物攻加一:
                    user.AbilityLvUpChanged(StatType.Atk, 1, false);
                    //user.AtkLVUp(1);
                    break;
                //case MoveEffect.成长:
                //    int up = 1;
                //    if (ground.Weather == Weather.Sunny)
                //        up = 2;

                //    user.AtkLVUp(up);
                //    user.SAtkLVUp(up);
                //    break;
                case MoveEffect.物攻加二:
                    user.AbilityLvUpChanged(StatType.Atk, 2, false);
                    //user.AtkLVUp(2);
                    break;
                case MoveEffect.物防加一:
                    user.AbilityLvUpChanged(StatType.Def, 1, false);
                    //user.DefLVUp(1);
                    break;
                case MoveEffect.物防加二:
                    user.AbilityLvUpChanged(StatType.Def, 2, false);
                    //user.DefLVUp(2);
                    break;
                case MoveEffect.特攻加一:
                    user.AbilityLvUpChanged(StatType.SpAtk, 1, false);
                    //user.SAtkLVUp(1);
                    break;
                case MoveEffect.特攻加二:
                    user.AbilityLvUpChanged(StatType.SpAtk, 2, false);
                    //user.SAtkLVUp(2);
                    break;
                //case MoveEffect.特攻加三:
                //    user.SAtkLVUp(3);
                //    break;
                case MoveEffect.特防加一:
                    user.AbilityLvUpChanged(StatType.SpDef, 1, false);
                    //user.SDefLVUp(1);
                    break;
                case MoveEffect.特防加二:
                    user.AbilityLvUpChanged(StatType.SpDef, 2, false);
                    //user.SDefLVUp(2);
                    break;
                case MoveEffect.特殊攻防加一:
                    user.AbilityLvUpChanged(StatType.SpAtk, 1, false);
                    user.AbilityLvUpChanged(StatType.SpDef, 1, false);
                    //user.SAtkLVUp(1);
                    //user.SDefLVUp(1);
                    break;
                case MoveEffect.物理攻防加一:
                    user.AbilityLvUpChanged(StatType.Atk, 1, false);
                    user.AbilityLvUpChanged(StatType.Def, 1, false);
                    //user.AtkLVUp(1);
                    //user.DefLVUp(1);
                    break;
                case MoveEffect.物攻速度加一:
                    user.AbilityLvUpChanged(StatType.Atk, 1, false);
                    user.AbilityLvUpChanged(StatType.Speed, 1, false);
                    //user.AtkLVUp(1);
                    //user.SpeedLVUp(1);
                    break;
                case MoveEffect.双防加一:
                    user.AbilityLvUpChanged(StatType.Def, 1, false);
                    user.AbilityLvUpChanged(StatType.SpDef, 1, false);
                    //user.DefLVUp(1);
                    //user.SDefLVUp(1);
                    break;
                case MoveEffect.变圆:
                    user.BattleState.defenseCurl = true;
                    //log.AppendText(user.CustomName + "变圆了！");
                    user.AbilityLvUpChanged(StatType.Def, 1, false);
                    //user.DefLVUp(1);
                    break;
                case MoveEffect.治愈之愿:
                    user.BattleState.healwish = true;
                    //log.AppendText(user.CustomName + "许下了治愈之愿！");
                    user.HurtFraction(1);
                    break;
                case MoveEffect.腹鼓:
                    if (user.HealthPower > user.MaxHp / 2)
                    {
                        user.HurtFraction(2);
                        log.AppendMsg("BellyDrum", user.CustomName);
                        //log.AddText(user.CustomName + "用HP换来了物攻的提升！");
                        user.AbilityLvUpChanged(StatType.Atk, 12, false);
                        //user.AtkLVUp(12, false);
                    }
                    else
                    {
                        Fail();
                    }
                    break;
                case MoveEffect.液体圈:
                    if (user.BattleState.aquaRing)
                    {
                        Fail();
                    }
                    else
                    {
                        user.BattleState.aquaRing = true;
                        log.AppendMsg("EnAquaRing", user.CustomName);
                        //log.AddText(user.CustomName + "被液体包围起来了！");
                    }
                    break;
                case MoveEffect.电磁浮游:
                    if (user.BattleState.suspension || Ground.Gravity)
                    {
                        Fail();
                    }
                    else
                    {
                        user.BattleState.suspension = true;
                        user.BattleState.suspensionCounter = 5;
                        log.AppendMsg("EnMagnetRise", user.CustomName);
                        //log.AddText(user.CustomName + "的特性变成了浮游！");
                    }
                    break;
                case MoveEffect.充电:
                    user.AbilityLvUpChanged(StatType.SpDef, 1, false);
                    //user.SDefLVUp(1);
                    user.BattleState.charge = true;
                    user.BattleState.chargeCounter = 2;
                    log.AppendMsg("Charge", user.CustomName);
                    //log.AddText(user.CustomName + "积蓄了电力！");
                    break;
                case MoveEffect.扎根:
                    if (user.BattleState.ingrain)
                    {
                        Fail();
                    }
                    else
                    {
                        user.BattleState.ingrain = true;
                        log.AppendMsg("EnIngrain", user.CustomName);
                        //log.AddText(user.CustomName + "长出了根！");
                    }
                    break;
                case MoveEffect.力量欺骗:
                    user.BattleState.powerTrick = !user.BattleState.powerTrick;
                    log.AppendMsg("PowerTrick", user.CustomName);
                    //log.AddText(user.CustomName + "交换了物攻和物防！");
                    break;
                case MoveEffect.新月之舞:
                    user.BattleState.lunarDance = true;
                    user.HurtFraction(1);
                    break;
                case MoveEffect.属性切换:
                    var conversion_Moves = user.GetSelMoves().ToArray();
                    user.Type1 = conversion_Moves[_random.Next(conversion_Moves.Length)].Type;
                    log.AppendMsg("TypeChange", user.CustomName, user.Type1);
                    //log.AddText(user.CustomName + "的属性变成了" + type.ToCnName());
                    break;
                case MoveEffect.怨念:
                    user.BattleState.grudge = true;
                    break;
                case MoveEffect.替身:
                    if (!user.MakeSubstitute())
                    {
                        //Fail();
                    }
                    break;
                case MoveEffect.能量储存:
                    if (user.BattleState.StockpileCounter < 3)
                    {
                        user.BattleState.StockpileCounter += 1;
                        log.AppendMsg("EnStockpile", user.CustomName, user.BattleState.StockpileCounter);
                        //log.AddText(user.CustomName + "储存了能量！");
                        if (user.DefenceLV < 6)
                        {
                            user.AbilityLvUpChanged(StatType.Def, 1, false);
                            user.BattleState.StockpileDefCounter++;
                        }
                        if (user.SpDefenceLV < 6)
                        {
                            user.AbilityLvUpChanged(StatType.SpDef, 1, false);
                            user.BattleState.StockpileSDefCounter++;
                        }
                        //if (user.DefLVUp(1, false))
                        //    user.BattleState.StockpileDefCounter += 1;
                        //if (user.SDefLVUp(1, false))
                        //    user.BattleState.StockpileSDefCounter += 1;
                    }
                    else
                    {
                        Fail();
                    }
                    break;
                case MoveEffect.能量吸入:
                    if (!user.CheckIfHPRecoveryIsEnabled())
                    {
                        return;
                    }
                    if (user.BattleState.StockpileCounter != 0 && user.HealthPower != user.MaxHp)
                    {
                        //switch (user.BattleState.StockpileCounter)
                        //{
                        //    case 1:
                        //        user.HPRecover(user.MaxHp * 0.25);
                        //        break;
                        //    case 2:
                        //        user.HPRecover(user.MaxHp * 0.5);
                        //        break;
                        //    case 3:
                        //        user.HPRecover(user.MaxHp);
                        //        break;
                        //}
                        user.HPRecoverFraction(2 << (3 - user.BattleState.StockpileCounter));
                        user.BattleState.StockpileCounter = 0;
                        user.AbilityLvDownChanged(StatType.Def, user.BattleState.StockpileDefCounter, false);
                        user.BattleState.StockpileDefCounter = 0;
                        user.AbilityLvDownChanged(StatType.SpDef, user.BattleState.StockpileSDefCounter, false);
                        user.BattleState.StockpileSDefCounter = 0;
                    }
                    else
                    {
                        Fail();
                    }
                    break;
                case MoveEffect.道具回收:
                    if (user.GetItem() != Item.None || user.UsedItem == Item.None || (112 < (int)user.UsedItem && (int)user.UsedItem < 116))
                    {
                        Fail();
                    }
                    else
                    {
                        log.AppendMsg("Recycle", user.CustomName, user.UsedItem);
                        //log.AddText(user.CustomName + "回收了道具！");
                        user.SetItem(user.UsedItem);
                        user.UsedItem = Item.None;
                        if (user.GetItem().IsRaiseItem())
                        {
                            user.RaiseItem();
                        }
                    }
                    break;
                case MoveEffect.保护色:
                    user.Type1 = Ground.Terrain.GetPokeType();
                    user.Type2 = BattleType.None;
                    log.AppendMsg("TypeChange", user.CustomName, user.Type1);
                    //log.AddText(user.CustomName + "的属性变成了" + user.Type1.ToCnName() + "！");
                    break;
                case MoveEffect.封印:
                    if (user.BattleState.imprison)
                    {
                        Fail();
                    }
                    else
                    {
                        user.BattleState.imprison = true;
                    }
                    break;
                case MoveEffect.跟我来:
                    if (Mode != BattleMode.Single)
                        Ground.followPokemon = user;
                    break;
            }
        }

    }
}
